package top.shiyiri.dao.impl;

import top.shiyiri.bean.Customer;
import top.shiyiri.dao.BaseDao;
import top.shiyiri.dao.CustomerDao;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;

/**
 * @author Aunean
 * @date 2022/1/19 21:37
 */
public class CustomerDaoImpl extends BaseDao<Customer> implements CustomerDao {
    @Override
    public void insertCustomer(Connection conn, Customer customer) {
        String sql = "insert into customers(name,email,birth) values(?,?,?)";
        update(conn, sql, customer.getName(), customer.getEmail(), customer.getBirth());
    }

    @Override
    public void deleteById(Connection conn, int id) {
        String sql = "delete from customers where id = ?";
        update(conn, sql, id);
    }

    @Override
    public void updateById(Connection conn, Customer customer) {
        String sql = "update customers set name=?,email=?,birth=? where id=?";
        update(conn, sql, customer.getName(), customer.getEmail(), customer.getBirth(), customer.getId());
    }

    @Override
    public Customer getCustomerById(Connection conn, int id) {
        String sql = "select id, name, email, birth from customers where id = ?";
        return getInstance(conn, sql, id);
    }

    @Override
    public List<Customer> getAllCustomerList(Connection conn) {
        String sql = "select id, name, email, birth from customers";
        return getForList(conn, sql);
    }

    @Override
    public Long getCount(Connection conn) {
        String sql = "select count(*) from customers";
        return getValue(conn, sql);
    }

    @Override
    public Date getMaxBirth(Connection conn) {
        String sql = "select max(birth) from customers";
        return getValue(conn, sql);
    }
}
